package com.cskaoyan.controller;

import com.cskaoyan.bean.bo.back.UpdateGoodsBO;
import com.cskaoyan.bean.bo.goodsbo.GoodsBo;
import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketGoods;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.ShowCatAndBrandByIdVO;
import com.cskaoyan.bean.vo.back.ShowCatAndBrandVO;
import com.cskaoyan.service.AdminGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: SSMItem
 * @description 该Controller实现商品管理
 * @author: YY
 * @create: 2022-10-12 21:03
 **/

@RestController
@RequestMapping("admin/goods")
public class AdminGoodsController {

    @Autowired
    AdminGoodsService adminGoodsService;

    /**
     * 查询显示所有商品信息
     * 请求url：http://localhost：8083/admin/goods/list
     * goodsSn：商品编号
     *
     * @return
     */
    @RequiresPermissions("admin:goods:list")
    @GetMapping("list")
    public BaseRespVo goodsList(BaseParam baseParam, Integer goodsSn, String name, Integer goodsId) {

        if (baseParam.getLimit() == 0) {

            List<MarketGoods> list = adminGoodsService.selectAllGoods(baseParam.getLimit());
            return BaseRespVo.ok(list);
        }

        //分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());

        List<MarketGoods> list = adminGoodsService.selectGoods(baseParam, goodsSn, name, goodsId);

        PageInfo<MarketGoods> goodsListVoPageInfo = new PageInfo<>(list);

        CommonData data = CommonData.data(goodsListVoPageInfo);

        return BaseRespVo.ok(data);

    }

    /***
     * 创建商品
     * 请求url：http://localhost：8083/admin/goods/create
     * @return
     */
    @RequiresPermissions("admin:goods:create")
    @PostMapping("create")
    public BaseRespVo createGoods(@RequestBody GoodsBo goodsBo) {

        adminGoodsService.insertGoods(goodsBo);

        return BaseRespVo.ok(null);

    }

    /**
     * 展示将被修改信息
     *
     * @return
     */
    @GetMapping("catAndBrand")
    public BaseRespVo showCatAndBrand() {
        ShowCatAndBrandVO showCatAndBrandVO = adminGoodsService.showCatAndBrand();
        return BaseRespVo.ok(showCatAndBrandVO);
    }

    /**
     * 通过id展示将被修改信息
     *
     * @return
     */
    @GetMapping("detail")
    public BaseRespVo showCatAndBrandById(Integer id) {
        ShowCatAndBrandByIdVO showCatAndBrandByIdVO = adminGoodsService.showCatAndBrandById(id);
        return BaseRespVo.ok(showCatAndBrandByIdVO);
    }

    /**
     * 更新商品
     *
     * @param updateGoodsBO
     * @return
     */
    @RequiresPermissions("admin:goods:update")
    @PostMapping("update")
    public BaseRespVo updateGoods(@RequestBody UpdateGoodsBO updateGoodsBO) {
        int code = adminGoodsService.updateGoods(updateGoodsBO);
        return BaseRespVo.ok();
    }
    @RequiresPermissions("admin:goods:delete")
    @PostMapping("delete")
    public BaseRespVo deleteGoods(@RequestBody MarketGoods marketGoods) {
        int code = adminGoodsService.deleteGoods(marketGoods);
        return BaseRespVo.ok();
    }
}
